package com.cincc.cinccimdemo.ui.conversation.mm;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.PreviewAdapter;
import com.cincc.cinccimdemo.common.MyUtils;
import com.cincc.cinccimdemo.model.MessageInfo;
import com.cincc.cinccimdemo.model.MessageInfoUI;
import com.cincc.cinccimdemo.viewmodel.ConversationViewModel;
import com.cincc.im.model.CollectModel;
import com.cincc.im.model.enums.MessageType;
import com.cincc.im.model.message.ImAudioMessage;
import com.cincc.im.model.message.ImFileMessage;
import com.cincc.im.model.message.ImImageMessage;
import com.cincc.im.model.message.ImMessage;
import com.cincc.im.model.message.ImTextMessage;
import com.cincc.im.model.message.ImVideoMessage;
import com.cincc.im.util.Base64Util;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PreviewActivity extends AppCompatActivity {

    private static final String TAG = "PreviewActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview_iv);
        init();
    }

    public static List<MessageInfoUI> mediaEntities;
    public static int currentPosition = 0;
    ConversationViewModel conversationViewModel;

    private void init() {
        conversationViewModel = new ViewModelProvider(this).get(ConversationViewModel.class);
        ViewPager2 viewPager = findViewById(R.id.viewpage_preview);
        PreviewAdapter previewAdapter = new PreviewAdapter(this, mediaEntities, currentPosition);
        viewPager.setAdapter(previewAdapter);
        viewPager.setCurrentItem(currentPosition, false);
        previewAdapter.setOnItemUpdateListener(message -> {
            conversationViewModel.updateConversationLiveData().postValue(message);
        });
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                previewAdapter.resetView();
            }
        });
        previewAdapter.setOnItemClickListener(this::finish);
    }


    public static void previewMedia(Context context, List<MessageInfoUI> list, MessageInfoUI uiMessage) {
        if (list == null || list.isEmpty()) {
            Log.d(TAG, "message is null or empty");
            return;
        }
        List<MessageInfoUI> mediaEntities = new ArrayList<>();
        int current = 0;
        int index = 0;
        for (MessageInfoUI message : list) {
            if (message.getMessageInfo().messageType == MessageType.IMAGE || message.getMessageInfo().messageType == MessageType.VIDEO) {
                mediaEntities.add(message);
                if (message.getMessageInfo().messageId.equals(uiMessage.getMessageInfo().messageId)) {
                    current = index;
                }
                index++;
            }
        }

        PreviewActivity.mediaEntities = mediaEntities;
        PreviewActivity.currentPosition = current;
        Intent intent = new Intent(context, PreviewActivity.class);
        context.startActivity(intent);
    }

    public static void previewImage(Context context, CollectModel collectModel) {
        List<MessageInfoUI> entries = new ArrayList<>();
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.messageId = collectModel.msgId;
        messageInfo.messageType = collectModel.messageType;
        messageInfo.messageContent = collectModel.messageContent;
        messageInfo.imSendType = collectModel.imSendType;
        messageInfo.senderId = collectModel.fromUserId;
        messageInfo.senderName = collectModel.fromUserName;
        messageInfo.groupId = collectModel.groupId;
        messageInfo.groupName = collectModel.groupName;
        try {
            String data = Base64Util.decodeToString(collectModel.messageContent);
            messageInfo.imMessage = MyUtils.getImMessage(collectModel.messageType, data);
        } catch (Exception e) {
            Log.e(TAG, "previewImage: " + e.getMessage());
        }
        MessageInfoUI entry = new MessageInfoUI(messageInfo);
        entries.add(entry);
        previewMedia(context, entries, entry);
    }
}